package com.gwz.util;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.gwz.context.ProContent;
import com.gwz.context.PropertiesMap;
import com.gwz.model.NewsModel;

/**
 * html生成工具类
 * @author panguixiang
 *
 */
public class HtmlUtil {

	//extendParams={"newsTempName":"A5NewsTemplate.html","newsFrom":"来自A5源码网"}
	/**
	 * 生成新闻html页面
	 * @param extendParams
	 * @param model
	 * @throws IOException
	 */
	public static void createNewsHtml(String extendParams,NewsModel model,String domain) throws IOException{
		if(StringUtils.isNotBlank(extendParams)) {
			JSONObject json = JSONObject.parseObject(extendParams);
			String htmlTempPath=PropertiesMap.getMapProValue(ProContent.STATIC_PATH).concat(ProContent.NEWS_TEMP_PATH)
					.concat(json.getString(ProContent.NEWS_TEMP_NAME_KEY));//获得新闻html模板文件
			File in = new File(htmlTempPath);
			String tempHtmlStr=StringUtils.EMPTY;
			Document tempDoc = Jsoup.parse(in,ProContent.EN_UNICODE,StringUtils.EMPTY);
			tempHtmlStr=tempDoc.html();
			tempHtmlStr = tempHtmlStr.replace(ProContent.NEWS_FROME,
					model.getType());//来源
			tempHtmlStr = tempHtmlStr.replace(ProContent.HTML_TITLE,
					model.getTitle());//标题
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.HTML_BIAOTI,
					model.getTitle());//大标题
			tempHtmlStr = tempHtmlStr.replace(ProContent.HTML_DESCRIPTION,
					model.getDescription());//描述
			tempHtmlStr = tempHtmlStr.replace(ProContent.HTML_KEYWORDS,
					model.getKey());//关键字
			String context = replaceRelaAddress(model.getContent(),domain);
			tempHtmlStr=tempHtmlStr.replaceAll(ProContent.HTML_CONTENT,context);//正文
			model.setUrl(ProContent.NEWS_PATH.concat(File.separator).concat(UUIDUtil.htmlName(model.getType())));
			model.setNews_template(json.getString(ProContent.NEWS_TEMP_NAME_KEY));
			String newsFilePath = PropertiesMap.getMapProValue(ProContent.STATIC_PATH).concat(model.getUrl());
			File file = new File(newsFilePath);
	        FileUtils.writeStringToFile(file,tempHtmlStr,ProContent.EN_UNICODE);
		}
	}
	
	
	public static void updateNewsHtml(String extendParams,NewsModel model,String domain) throws IOException{
		if(StringUtils.isNotBlank(extendParams)) {
			JSONObject json = JSONObject.parseObject(extendParams);
			String htmlTempPath=PropertiesMap.getMapProValue(ProContent.STATIC_PATH).concat(ProContent.NEWS_TEMP_PATH)
					.concat(json.getString(ProContent.NEWS_TEMP_NAME_KEY));//获得新闻html模板文件
			File in = new File(htmlTempPath);
			String tempHtmlStr=StringUtils.EMPTY;
			Document tempDoc = Jsoup.parse(in,ProContent.EN_UNICODE,StringUtils.EMPTY);
			tempHtmlStr=tempDoc.html();
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.NEWS_FROME,
					model.getType());//来源
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.HTML_TITLE,
					model.getTitle());//标题
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.HTML_BIAOTI,
					model.getTitle());//大标题
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.HTML_DESCRIPTION,
					model.getDescription());//描述
			tempHtmlStr = tempHtmlStr.replaceAll(ProContent.HTML_KEYWORDS,
					model.getKey());//关键字
			String context = replaceRelaAddress(model.getContent(),domain);
			tempHtmlStr=tempHtmlStr.replaceAll(ProContent.HTML_CONTENT,context);//正文
			model.setNews_template(json.getString(ProContent.NEWS_TEMP_NAME_KEY));
			String newsFilePath =  PropertiesMap.getMapProValue(ProContent.STATIC_PATH).concat(model.getUrl());
			File file = new File(newsFilePath);
	        FileUtils.writeStringToFile(file,tempHtmlStr,ProContent.EN_UNICODE);//再生成
		}
		
	}
	private static String replaceRelaAddress(String contextStr,String domain) throws MalformedURLException {
		Document contextDoc = Jsoup.parse(contextStr);
		absoluteImagSrc(contextDoc.select("img[src]"),domain);
		absoluteAhref(contextDoc.select("a[href]"),domain);
		return contextDoc.html();
	}
	
	/**
	 * 将相对路径Img src 装换为绝对的url  
	 * @param imagElements
	 * @param domain
	 * @throws MalformedURLException
	 */
    public static void absoluteImagSrc(Elements imagElements, String domain) throws MalformedURLException{  
        Iterator<Element> iterator=imagElements.iterator();
        String src=StringUtils.EMPTY;
        while(iterator.hasNext()){  
            Element element=iterator.next();  
            src=element.attr("src");//将所有的相对地址换为绝对地址;
            if(src.indexOf(ProContent.HTTP)>-1) {
            	continue;
            }
            element.attr("src",domain.concat(src));//装换为  
        }  
    }
    
    
    /**
	 * 将相对路径A href 装换为绝对的url  
	 * @param imagElements
	 * @param domain
	 * @throws MalformedURLException
	 */
    public static void absoluteAhref(Elements imagElements, String domain) throws MalformedURLException{  
        Iterator<Element> iterator=imagElements.iterator();
        String src=StringUtils.EMPTY;
        while(iterator.hasNext()){  
            Element element=iterator.next();  
            src=element.attr("href");//将所有的相对地址换为绝对地址;
            if(src.indexOf(ProContent.HTTP)>-1) {
            	continue;
            }
            element.attr("href",domain.concat(src));//装换为  
        }  
    }
}
